package filter;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;

import javax.imageio.ImageIO;

public class FindLetters {

	public static void main(String[] args) throws IOException {
		BufferedImage img = ImageIO.read(NewSobel.class
				.getResource("/resource/paisagem1.jpg"));
		long l = System.currentTimeMillis();

		img = NewSobel.applyThreshold(NewSobel.apply(img), 20);
		find(img);
		System.out.println(System.currentTimeMillis() - l);
		

		int[][] segments = find2(img);
		printSegments(segments);

		ImageIO.write(img, "png", new File("segm1.png"));
		ImageIO.write(find1(img), "png", new File("segm2.png"));
		ImageIO.write(find(img), "png", new File("segm3.png"));
	}

	private static BufferedImage find(BufferedImage img) {
		int w = img.getWidth();
		int h = img.getHeight();
		BufferedImage tmp = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
		Graphics2D g2d = (Graphics2D) tmp.getGraphics();
		int[][] segments = find2(img);
		int parts = 30;
		int xparts = w / parts;
		int yparts = h / parts;

//		for (int y = 0; y < segments.length; y++) {
//			for (int x = 0; x < segments[y].length; x++) {
//				if (segments[y][x] == 1) {
//					g2d.setColor(Color.RED);
//				} else {
//					g2d.setColor(Color.BLACK);
//				}
//				g2d.fillRect(x * xparts, y * yparts, parts, parts);
//			}
//		}
		for (int y = 0; y < segments.length; y++) {
			for (int x = 0; x < segments[y].length; x++) {
				if (segments[y][x] == 1) {
					int step = 0;
					int ypoint1 = 0;
					int ypoint2 = 0;
					for(int y1 = y; y1 < segments.length; y1++) {
						if(step == 0) {
							if (segments[y1][x] == 0) {
								step = 1;
								ypoint1 = y1;
							}
						} else {
							if (segments[y1][x] == 1) {
								step = 2;
								ypoint2 = y1;
								break;
							}
						}
					}
					if(step == 2) {
						g2d.setColor(Color.RED);
						g2d.fillRect(x * xparts, (ypoint1 + ((ypoint2 - ypoint1) / 2)) * yparts, parts, parts);
					}
				}
			}
		}
		return tmp;
	}

	private static BufferedImage find1(BufferedImage img) {
		int w = img.getWidth();
		int h = img.getHeight();
		BufferedImage tmp = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
		int parts = 30;
		int xparts = w / parts;
		int yparts = h / parts;
		Graphics2D g2d = (Graphics2D) tmp.getGraphics();
		for (int x = 0; x < w; x += xparts) {
			for (int y = 0; y < h; y += yparts) {
				double porcentEgdes = porcentEgdes(img, x, y, x + xparts - 1, y
						+ yparts - 1);

				if (porcentEgdes < 0.1d) {
					g2d.setColor(new Color(255, 0, 0));
				} else {
					g2d.setColor(new Color((int) (porcentEgdes * 255),
							(int) (porcentEgdes * 255),
							(int) (porcentEgdes * 255)));
				}
				g2d.fillRect(x, y, xparts, yparts);
			}
		}
		return tmp;
	}

	private static int[][] find2(BufferedImage img) {
		int w = img.getWidth();
		int h = img.getHeight();
		int parts = 30;
		int xparts = w / parts;
		int yparts = h / parts;
		int[][] segments = new int[parts + 1][parts + 1];
		int sx = 0;
		int sy = 0;

		for (int x = 0; x < w; x += xparts) {
			sy = 0;
			for (int y = 0; y < h; y += yparts) {
				double porcentEgdes = porcentEgdes(img, x, y, x + xparts - 1, y
						+ yparts - 1);

				if (porcentEgdes < 0.1d) {
					segments[sy][sx] = 1;
				} else {
					segments[sy][sx] = 0;
				}
				sy++;
			}
			sx++;
		}

		return segments;
	}

	private static void printSegments(int[][] segments) {
		for (int[] segment : segments) {
			System.out.println(Arrays.toString(segment));
		}
	}

	private static double porcentEgdes(BufferedImage img, int x1, int y1,
			int x2, int y2) {
		double egdes = 0;
		double n = 0;
		int w = img.getWidth();
		int h = img.getHeight();

		int white = Color.WHITE.getRGB();
		for (int x = x1; x < x2; x++) {
			for (int y = y1; y < y2; y++) {
				if (x < w && y < h) {
					if (img.getRGB(x, y) == white) {
						egdes++;
					}
					n++;
				}
			}
		}

		return egdes / n;
	}
}
